<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\CommonModel;
use App\Models\HomeModel;
use Storage;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class ProductController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';
    protected $type_id = 2;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(){

    }
    public function index(){

    }
    
    public function show($id){
        if($id){
            $product = HomeModel::getProduct(['id','name','weight','price','deposit','info','refund','content'],['id'=>$id,'is_show'=>1]);
            $images = HomeModel::getImages(['id','img_url'],['status'=>0,'pid'=>$id]);
            // dd($images);
        }
        if($product){
            // dd($product->id);
            $res = CommonModel::inc('product','click',['id'=>$product->id]);
            // dd($res);
        }
        return view('home/product/show',['product'=>$product,'images'=>$images,'type_id'=>$this->type_id]);
    }
    public function goBuy(Request $request){
        // echo 123;exit;
        $data = $request->input();
        $id = intval($data['id']);
        $num = $data['num'];
        // dd($data);
        if(!empty(session('user'))){
            $uid = session('user')->id;
            $product = HomeModel::getProduct(['deposit','name','price'],['id'=>$id,'is_show'=>1]);
            // dd($product);
            if($product){
                $deposit = $product->deposit;
                $money = floatval($deposit)*$num;
            }
            $order['uid'] = $uid;
            $order['pid'] = $id;
            if($num!=1){
                $order['num'] = $num;
            }
            $order['add_time'] = time();
            $order['money'] = $money;
            $order['name'] = $product->name;
            $order['price'] = $product->price;
            $order['order'] = date('YmdHis',$order['add_time']).rand_string();
            $order['add_time'] = time();
            // dd($order);
            $newid = CommonModel::addDataGetId('order',$order);
            if($newid){
                return response()->json(['res'=>1,'msg'=>'成功！','jumpUrl'=>'/user/playTure/'.$newid]);
            }else{
                return response()->json(['res'=>0,'msg'=>'提交订单失败！']);
            }
        }else{
            return response()->json(['res'=>2,'msg'=>'请您登录！']);
            // return redirect('/message')->with(['res'=>0,'msg'=>'请您登录','url'=>'/']);
        }
        

    }

}
